package views.games;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import views.util.GameColors;

/**
 * Le view dovrebbero implementare questa interfaccia, il metodo update aggiorna
 * lo stato della view sulla base delle informazioni prese dai getter del model
 */
public abstract class GameView extends JPanel {

	protected JLabel gameClock; // può rappresentare la casella con il timer o i
								// turni
	protected JLabel score;
	protected JLabel other;

	/**
	 * crea un JPanel con BorederLayout e le tre caselle in fondo.
	 * 
	 * @param backgroundColor
	 * @param tileColor
	 *            colore delle tre caselle in basso
	 * @param gameClock
	 * @param score
	 * @param other
	 */
	public GameView(Color backgroundColor, Color tileColor, String gameClock,
			String score, String other) {
		this.gameClock = newLabel(gameClock, tileColor);
		this.score = newLabel(score, tileColor);
		this.other = newLabel(other, tileColor);
		setPreferredSize(new Dimension(800, 680));
		setLayout(new BorderLayout());
		setBackground(backgroundColor);
		JPanel bottom = new JPanel();
		bottom.setLayout(new GridBagLayout());
		bottom.setOpaque(false);
		GridBagConstraints gbcBottom = new GridBagConstraints();
		add(bottom, BorderLayout.SOUTH);
		gbcBottom.insets = new Insets(2, 3, 45, 2);
		gbcBottom.gridx = 0;
		gbcBottom.gridy = 0;
		bottom.add(this.gameClock, gbcBottom);
		gbcBottom.gridx = 1;
		gbcBottom.gridy = 0;
		bottom.add(this.score, gbcBottom);
		gbcBottom.gridx = 2;
		gbcBottom.gridy = 0;
		bottom.add(this.other, gbcBottom);
		bottom.setPreferredSize(new Dimension(800, 100));
	}

	public abstract void gameEnded();

	protected JLabel newLabel(String text, Color c) {
		JLabel label = new JLabel() {
			@Override
			public void setPreferredSize(Dimension dim) {
				super.setPreferredSize(dim);
				disableEvents(AWTEvent.INVOCATION_EVENT_MASK);
			}
		};

		Dimension dim = new Dimension(200, 50);
		label.setHorizontalAlignment(JTextField.CENTER);
		label.setPreferredSize(dim);
		label.setOpaque(true);
		label.setBackground(c);
		label.setForeground(new Color(48, 48, 48));
		label.setText(text);
		Font f = new Font("Segoe UI", Font.BOLD, 18);
		label.setFont(f);
		return label;
	}

	protected JButton newButton(String text) {
		JButton button = new JButton();
		Dimension dim = new Dimension(200, 50);
		button.setHorizontalAlignment(JTextField.CENTER);
		button.setPreferredSize(dim);
		button.setOpaque(true);
		button.setBackground(GameColors.GRIGIO_SCURO);
		button.setForeground(Color.WHITE);
		button.setText(text);
		Font f = new Font("Segoe UI", Font.BOLD, 18);
		button.setFont(f);
		return button;
	}

	public void setScore(int s) {
		score.setText("PUNTEGGIO: " + s);
	}

	public void setOther(String s) {
		other.setText(s);
	}

	public void setGameClock(int r) {
		gameClock.setText("TURNO " + r + " su 15");
		;
	}

	public void setOtherThanClock(String s) {
		gameClock.setText(s);
	}

	public void checkFocusMain() {
	}

}
